﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EvaluationSystem.Exceptions;
using System.Data;
using System.Web.UI;

namespace EvaluationSystem.DAO
{
    public class Tabs
    {
        public int Id { get; set; }
        public string TabName { get; set; }
        public string RoleName { get; set; }
    }

    public class TabsManager : DAO
    {
        const string InsertTabCommand = " insert into [Tabs] ([TabName], [RoleName]) values(@TabName,@RoleName) ";
        const string SelectTabByTabNameCommand = " select [Id],[TabName] from [Tabs] where [TabName]=@TabName ";
        const string DeleteTabByTabIdCommand = " delete [Tabs] where [Id]=@Id ";
        const string GetTabsCountCommand = " select count([Id]) from [Tabs] ";
        const string SelectAllTabsCommand = " select [Id], [TabName],[RoleName] from [Tabs] ";
        const string UpdateTabsByIdCommand = " update [Tabs] set [TabName]=@TabName, [RoleName]=@RoleName where [Id]=@Id ";
        const string SelectTabExceptMeByIdCommand = " select [Id] from [Tabs] where [TabName]=@tabName and [Id]<>@Id ";


        public bool CheckExistTabByTabIdExceptByMe(string id, string tabName)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>(2);
            parameters["Id"] = id;
            parameters["TabName"] = tabName;
            return ExcuteCommandDataTable(SelectTabExceptMeByIdCommand, parameters).Rows.Count > 0;
        }
        public bool UpdateTabById(string id, string tabName, string roleName)
        {
            if (CheckExistTabByTabIdExceptByMe(id,tabName))
            {
                throw new ExistTabException();
            }
            Dictionary<string, object> parameters = new Dictionary<string, object>(3);
            parameters["TabName"] = tabName;
            parameters["RoleName"] = roleName;
            parameters["Id"] = id;
            return ExcuteCommandNonQuery(UpdateTabsByIdCommand, parameters) > 0;
        }
        public DataTable GetTabByTabName(string tabName)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>(1);
            parameters["TabName"] = tabName;
            return ExcuteCommandDataTable(SelectTabByTabNameCommand, parameters);
        }
        public DataTable GetAllTabs()
        {
            return ExcuteCommandDataTable(SelectAllTabsCommand, null);
        }

        public DataTable GetAllGroups()
        {
            string getAllGroupCommand = "select [Id],[GroupName] from [Groups]";
            return ExcuteCommandDataTable(getAllGroupCommand, null);
        }

        public int GetTabsCount(Page page, bool isRefresh = false)
        {
            if (page.Session[Constants.TabsCount] != null && !isRefresh)
            {
                return (int)page.Session[Constants.TabsCount];
            }
            else
            {
                DataTable dt = ExcuteCommandDataTable(GetTabsCountCommand, null);
                int count = 0;
                if (dt.Rows.Count > 0)
                {
                    count = (int)dt.Rows[0][0];
                }
                return count;
            }
        }
        public bool DeleteTabByTabId(string id)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters["Id"] = id;
            return ExcuteCommandNonQuery(DeleteTabByTabIdCommand, parameters) > 0;
        }
        public bool CheckExistTabByTabName(string tabName)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>(1);
            parameters["TabName"] = tabName;
            return ExcuteCommandDataTable(SelectTabByTabNameCommand, parameters).Rows.Count > 0;
        }

        public bool AddTab(string tabName, string roleName)
        {
            if (CheckExistTabByTabName(tabName))
            {
                throw new ExistTabException();
            }
            Dictionary<string, object> parameters = new Dictionary<string, object>(2);
            parameters["TabName"] = tabName;
            parameters["RoleName"] = roleName;
            return ExcuteCommandNonQuery(InsertTabCommand, parameters) > 0;
        }
    }
}